View Javadoc

1   ////////////////////////////////////////////////////////////////////////////////
2   //CabaWeb
3   //Copyright (C) 2004  Thomas Vogt <Thomas.Vogt@TVC-Software.com>
4   //
5   //Original LocaleAction
6   //Copyright (C) 1999  David Wintefeldt
7   //
8   //This library is free software; you can redistribute it and/or
9   //modify it under the terms of the GNU Lesser General Public
10  //License as published by the Free Software Foundation; either
11  //version 2.1 of the License, or (at your option) any later version.
12  //
13  //This library is distributed in the hope that it will be useful,
14  //but WITHOUT ANY WARRANTY; without even the implied warranty of
15  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  //Lesser General Public License for more details.
17  //
18  //You should have received a copy of the GNU Lesser General Public
19  //License along with this library; if not, write to the Free Software
20  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  ////////////////////////////////////////////////////////////////////////////////
22  
23  package org.fhw.cabaweb.webfrontend.actions;
24  
25  import java.util.Locale;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  import javax.servlet.http.HttpSession;
30  
31  import org.apache.commons.beanutils.PropertyUtils;
32  import org.apache.commons.logging.Log;
33  import org.apache.commons.logging.LogFactory;
34  import org.apache.struts.Globals;
35  import org.apache.struts.action.Action;
36  import org.apache.struts.action.ActionForm;
37  import org.apache.struts.action.ActionForward;
38  import org.apache.struts.action.ActionMapping;
39  
40  /***
41   * <strong>Action</strong>-Klasse f&uuml;r die Locale Action .
42   * Wechseln der Sprache (locale) anhand eines Sprachkürzels.
43   * Die Controller Klasse der Struts Model View Controller Architektur.
44   *
45   * Im Bezug auf Umleitung auf den Referrer anstatt auf die Indexseite geändert.
46   * Basiert auf Source von David Wintefeldt (Struts Tutorial Application)
47   *
48   * ORIGINAL LICENSE
49   *
50   * The Apache Software License, Version 1.1
51   *
52   * Copyright (c) 1999 The Apache Software Foundation.  All rights
53   * reserved.
54   *
55   * Redistribution and use in source and binary forms, with or without
56   * modification, are permitted provided that the following conditions
57   * are met:
58   *
59   * 1. Redistributions of source code must retain the above copyright
60   *    notice, this list of conditions and the following disclaimer.
61   *
62   * 2. Redistributions in binary form must reproduce the above copyright
63   *    notice, this list of conditions and the following disclaimer in
64   *    the documentation and/or other materials provided with the
65   *    distribution.
66   *
67   * 3. The end-user documentation included with the redistribution, if
68   *    any, must include the following acknowlegement:
69   *       "This product includes software developed by the
70   *        Apache Software Foundation (http://www.apache.org/)."
71   *    Alternately, this acknowlegement may appear in the software itself,
72   *    if and wherever such third-party acknowlegements normally appear.
73   *
74   * 4. The names "The Jakarta Project", "Struts", and "Apache Software
75   *    Foundation" must not be used to endorse or promote products derived
76   *    from this software without prior written permission. For written
77   *    permission, please contact apache@apache.org.
78   *
79   * 5. Products derived from this software may not be called "Apache"
80   *    nor may "Apache" appear in their names without prior written
81   *    permission of the Apache Group.
82   *
83   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
84   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
85   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
86   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
87   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
88   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
89   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
90   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
91   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
92   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
93   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
94   * SUCH DAMAGE.
95   * ====================================================================
96   *
97   * This software consists of voluntary contributions made by many
98   * individuals on behalf of the Apache Software Foundation.  For more
99   * information on the Apache Software Foundation, please see
100  * <http://www.apache.org/>.
101  *
102  * @author  Jon Howell <jonh@cs.dartmouth.edu> and <a href="mailto:thomas.vogt@tvc-software.com">Thomas Vogt</a>
103  * @version 1999 - Changes 02.06.2004
104  */
105 public final class LocaleAction extends Action
106 {
107 
108     /*** Commons Logging Instanz */
109     private Log log = LogFactory.getLog("org.fhw.cabaweb.webfrontend.actions.LocaleAction");
110 
111 	/***
112 	 * Verarbeiten der spezifizierten HTTP Anfrage und erzeugen der zugeordneten
113 	 * HTTP Antwort bzw. Forwarden an eine andere Web Komponente, die die Antwort
114 	 * erzeugt.
115 	 * Gibt eine <code>ActionForward</code> Instanz zurück die angibt wohin und wie
116 	 * die Kontrolle weitergegeben werden soll. Kann auch <code>null</code> sein,
117 	 * wenn die Anfrage bereits bearbeitet wurde.
118 	 * @exception Exception wenn ein Eingabe-/Ausgabe Fehler auftritt oder eine Servlet Exception auftritt
119 	 * bzw. die Business Logik einen Fehler verursacht
120 	 * @param response The HTTP Antwort die wir erzeugen
121 	 * @param form Das optionale ActionForm Bean für die Anfrage (soweit vorhanden)
122 	 * @param mapping Das ActionMapping das benutzt wurde um diese Instanz zu selektieren
123 	 * @param request Die HTTP Anfrage die wir gerade bearbeiten
124 	 * @return Die Action zu der wir weiterleiten
125 	 */
126     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception
127     {
128         /*** Die Session der aktuellen Anfrage */
129         HttpSession session = request.getSession();
130         /*** Die Locale Variable (Sprache, Formatierung, etc.) */
131         Locale locale = getLocale(request);
132         /*** Die Sprache in das die Locale geändert werden soll */
133         String language = null;
134         /*** Das Land in das die Locale geändert werden soll */
135         String country = null;
136         /*** Variable in der das Redirecting gespeichert wird */
137         ActionForward redirectTo = null;
138 
139         try
140         {
141             language = (String) PropertyUtils.getSimpleProperty(form, "language");
142             country = (String) PropertyUtils.getSimpleProperty(form, "country");
143         }
144         catch (Exception e)
145         {
146             log.error(e.getMessage(), e);
147         }
148 
149         if (log.isDebugEnabled())
150         {
151             log.debug(" Language: " + language);
152             log.debug(" Country: " + country);
153         }
154 
155         if ((language != null && language.length() > 0) && (country != null && country.length() > 0))
156         {
157             locale = new java.util.Locale(language, country);
158         }
159         else if (language != null && language.length() > 0)
160         {
161             locale = new java.util.Locale(language, "");
162         }
163 
164         session.setAttribute(Globals.LOCALE_KEY, locale);
165 
166         if (log.isDebugEnabled())
167         {
168             log.debug(" Global Language: " + locale.getLanguage());
169             log.debug(" Global Country: " + locale.getCountry());
170         }
171 
172         // Get the Referer from request Header
173         String referer = request.getHeader("Referer");
174 
175         // Extract the mapping name from Referer like "/main.do"
176         String mappingName = referer.substring(referer.lastIndexOf("/"));
177 
178         // some DPU(DAU) presses change language button twice filter out "/locale.do"
179         if (mappingName.indexOf("locale.do") >= 0)
180         {
181             if (log.isDebugEnabled())
182             {
183                 log.debug(" Referer: " + referer);
184                 log.debug(" Redirecting to: \"success\" because Referer is locale.do");
185             }
186 
187             // Forward to the success page
188             redirectTo = mapping.findForward("success");
189         }
190         else
191         {
192             if (log.isDebugEnabled())
193             {
194                 log.debug(" Referer: " + referer);
195                 log.debug(" Redirecting to: " + mappingName);
196             }
197 
198             // Forward to the previous referer page
199             redirectTo = new ActionForward(mappingName);
200             redirectTo.setRedirect(true);
201         }
202 
203         return redirectTo;
204     }
205 }